﻿@using SmartStore.PayPal
@using SmartStore.PayPal.Models
@model PayPalPlusCheckoutModel
<script src="https://www.paypalobjects.com/webstatic/ppplus/ppplus.min.js" type="text/javascript"></script>
<script type="text/javascript">
	jQuery(document).ready(function () {

		var payPalPlus = PAYPAL.apps.PPP({
			approvalUrl: "@Html.Raw(Model.ApprovalUrl)",
			placeholder: "PayPalPlusPaymentWall",
			mode: "@(Model.UseSandbox ? "sandbox" : "live")",
			country: "@(Model.BillingAddressCountryCode ?? "DE")",
			buttonLocation: "outside",
			language: "@Html.Raw(Model.LanguageCulture ?? "de_DE")",
			useraction: "continue",
			//surcharging: true,	// insufficient and possibly wrong message
			showLoadingIndicator: true,
			enableContinue: "PayPalPlusNextCheckoutStep",
			disableContinue: "PayPalPlusNextCheckoutStep",
			onLoad: function () {
				$('#PayPalPlusNextCheckoutStep').fadeIn();
				pppDisplayPaymentFees(null, true);
			},
			onThirdPartyPaymentMethodSelected: function (data) {
				pppDisplayPaymentFees(data.thirdPartyPaymentMethod, true);
			},
			onThirdPartyPaymentMethodDeselected: function (data) {
				pppDisplayPaymentFees(null, false);
			},
			@if (Model.UseSandbox)
			{
				<text>showPuiOnSandbox: true,</text>
			}
			@if (Model.ThirdPartyPaymentMethods.Any())
			{
				<text>thirdPartyPaymentMethods: [</text>
				foreach (var method in Model.ThirdPartyPaymentMethods.OrderBy(x => x.DisplayOrder))
				{
					<text>{ redirectUrl: "</text>@Html.Raw(method.RedirectUrl)<text>",</text>
					<text>methodName: </text>@Html.Raw(method.MethodName.EncodeJsString())<text>,</text>
					if (method.ImageUrl.HasValue())
					{
					<text>imageUrl: "</text>@Html.Raw(method.ImageUrl)<text>",</text>
					}
					if (method.Description.HasValue())
					{
					<text>description: </text>@Html.Raw(method.Description)<text>,</text>
					}
					<text> }, </text>
				}
				<text>]</text>
			}
		});

		// next button clicked
		$('#PayPalPlusNextCheckoutStep').click(function (e) {
			e.preventDefault();

			// patch shipping data
			$({}).doAjax({
				type: 'POST',
				url: '@Url.Action("PatchShipping", "PayPalPlus", new { area = Plugin.SystemName })',
				callbackSuccess: function (resp) {
					if (resp.success) {
						PAYPAL.apps.PPP.doCheckout();
					}
					else if (resp.reload) {
						window.location.reload(true);
					}
					else {
						window.displayNotification(resp.error, 'error');
					}
				}
			});
			return false;
		});

		function pppDisplayPaymentFees (method, selected) {
			try {
				var hasAnyFees = @(Model.HasAnyFees.ToString().ToLower());
				if(!hasAnyFees) {
					return;
				}

				setTimeout(function() {
					if (!method && payPalPlus.getPaymentMethod) {
						method = payPalPlus.getPaymentMethod();
					}

					var fee = '',
						pppPaymentFee = '@Model.PayPalFee',
						pppThirdPartyPaymentFees = [@Html.Raw(Model.ThirdPartyFees)];

					if (0 === method.lastIndexOf('pp-', 0)) {
						fee = pppPaymentFee;
					}
					else {
						if (method && method.length > 0 && !$('#PayPalPlusNextCheckoutStep').is(':disabled')) {
							method = method.replace("'", "");
							for (var i=0; i < pppThirdPartyPaymentFees.length; ++i) {
								if (pppThirdPartyPaymentFees[i][0] === method) {
									fee = pppThirdPartyPaymentFees[i][1];
									break;
								}
							}
						}
					}

					$('#PayPalPlusPaymentFeeContainer').css('visibility', fee.length === 0 ? 'hidden' : 'visible');
					$('#PayPalPlusFeeAmount').text(fee);
				}, 500);
			}
			catch(e) {
				console.log(e.description);
			}
		}
	});
</script>